package com.atguigu.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * className: MyController
 * description:
 * date: 2024/6/18-10:34
 * <p>
 * project: shiro
 * package: com.atguigu.shiro.controller
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Controller
@RequestMapping("myController")
public class MyController {

    /**
     * 跳转登录页面
     *
     * @return {@link String}
     */
    @GetMapping("login")
    public String login() {
        return "login";
    }

    /**
     * 登录认证
     *
     * @param name {@link String}
     * @param pwd  {@link String}
     * @return {@link String}
     */
    @GetMapping("userLogin")
    public String userLogin(String name, String pwd,
                            @RequestParam(defaultValue = "false") boolean rememberMe,
                            HttpSession session) {
        // 1 获取 Subject 对象
        Subject subject = SecurityUtils.getSubject();
        // 2 封装请求数据到 token 对象中
        AuthenticationToken token = new
                UsernamePasswordToken(name, pwd, rememberMe);
        // 3 调用 login 方法进行登录认证
        try {
            subject.login(token);
            session.setAttribute("user", token.getPrincipal().toString());
            return "main";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("登录失败");
            return "登录失败";
        }
    }

    /**
     * 登录认证验证 rememberMe
     *
     * @return {@link String}
     */
    @GetMapping("userLoginRm")
    public String userLogin(HttpSession session) {
        session.setAttribute("user", "rememberMe");
        return "main";
    }

    /**
     * 登录认证验证角色
     *
     * @return {@link String}
     */
    @RequiresRoles("admin")
    @GetMapping("userLoginRoles")
    @ResponseBody
    public String userLoginRoles() {
        System.out.println("登录认证验证角色");
        return "验证角色成功";
    }

    /**
     * 登录认证验证权限
     *
     * @return {@link String}
     */
    @RequiresPermissions("user:delete")
    @GetMapping("userPermissions")
    @ResponseBody
    public String userLoginPermissions() {
        System.out.println("登录认证验证权限");
        return "验证权限成功";
    }
}
